<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/4 0004
 * Time: 17:48
 */
namespace app\wap\controller;
use think\Db;


class works extends Base
{
    public function index(){
        session('cg_id',null);
        session('venue_id',null);
        session('style_id',null);
        //风格
        $data_style = Db::name('style')->where('is_show=1 and sign="mobile" and is_del = 0')->order('sort desc,id desc')->select();
        $this->assign('data_style',$data_style);
        //基地
        $data_jds = Db::name('jd')->where('is_show=1 and sign="mobile" and is_del = 0')->order('sort desc,id desc')->select();
        $this->assign('data_jds',$data_jds);
        //场馆
        $data_cg = Db::name('venue')->where('is_show=1 and sign="mobile" and is_del = 0')->order('sort desc,id desc')->select();
        $this->assign('data_cg',$data_cg);
        //作品

        $where = 'is_del=0 and is_show=1 and sign="mobile"';
        if(input('param.id')){
            $str_ids = explode('-',input('param.id'));
            if($str_ids[0] && intval($str_ids[0]) > 0){
                $style_id = intval($str_ids[0]);
                $where .= " and style_id = ".$style_id;
                session('style_id',$style_id);
            }
            if($str_ids[1] && intval($str_ids[1]) > 0){
                $jd_id = intval($str_ids[1]);
                if(session('cg_id') == $jd_id){
                    session('cg_id',null);
                }else{
                    $where .= " and cg_id = ".$jd_id;
                    session('cg_id',$jd_id);
                }


            }
            if($str_ids[2] && intval($str_ids[2]) > 0){
                $cg_id = intval($str_ids[2]);
                $where .= " and venue_id = ".$cg_id;
                session('venue_id',$cg_id);
            }
        }



        $this->assign('SESSION',isset($_SESSION)?$_SESSION:[]);
        $data = Db::name('zp')->where($where)->order('sort desc,id desc')->limit(10)->select();
        //判断第一页的数量是否少于总的数量(可以得到是否有第二页的数据)
        $type =1;
        $next_data= Db::name('zp')->where($where)->page(2,10)->select();
        if(!$next_data){
            $type =2;
        }

        $this->assign('type',$type);
        $this->assign('data',$data);
        $this->assign('data_seo',getColumnseo(2));
        return $this->fetch();

    }


    public function nr(){
        $id = intval(input('param.id'));
        if($id){
            $data = Db::name('zp')->where('id',$id)->find();
            if($data['works_photo'] !==''){
                $imgurl = Db::name('works_pic')->where('pic_id','in',$data['works_photo'])->select();
                $data['imgurl']=$imgurl;
            }
            $this->assign('data',$data);
        }

        //上一组
        $upId = Db::name('zp')->where('id','<',$id)->where('is_del=0 and is_show=1 and sign="mobile"')->order('id desc')->limit(1)->value('id');
        $this->assign('upId',$upId);
        //下一组
        $downId = Db::name('zp')->where('id','>',$id)->where('is_del=0 and is_show=1 and sign="mobile"')->order('id asc')->limit(1)->value('id');
        $this->assign('downId',$downId);
        return $this->fetch();
    }


    public function getMore(){
        $page = intval(input('post.page'));
        $strid = input('post.strid');


        $where = 'is_del=0 and is_show=1 and sign="mobile"';
        if($strid){
            $str_ids = explode('-',$strid);

           // dump($str_ids);
            if($str_ids[0] && intval($str_ids[0]) > 0){
                $style_id = intval($str_ids[0]);
                $where .= " and style_id = ".$style_id;

            }
            if($str_ids[1] && intval($str_ids[1]) > 0){
                $jd_id = intval($str_ids[1]);
                $where .= " and cg_id = ".$jd_id;


            }
            if($str_ids[2] && intval($str_ids[2]) > 0){
                $cg_id = intval($str_ids[2]);
                $where .= " and venue_id = ".$cg_id;

            }
        }

        $type =1;
        $res= Db::name('zp')->where($where)->page($page,10)->select();


        $next_data= Db::name('zp')->where($where)->page($page+1,10)->select();
        if(!$next_data){
            $type =2;
        }
        if($res){
            return json(['code'=>1,'data'=>$res,'type'=>$type]);
        }else{
            return json(['code'=>0,'msg'=>'没有更多了']);
        }

    }


}